<template>
  <el-dialog 
    v-model="visible" 
    title="选择壁纸" 
    width="900px" 
    top="5vh"
    :lock-scroll="true"
    append-to-body
  >
    <div class="wallpaper-dialog-content">
      <el-tabs v-model="activeTab" class="fixed-tabs">
        <!-- 静态壁纸 -->
        <el-tab-pane label="静态壁纸" name="static">
          <div class="tab-content-wrapper">
            <div class="wallpaper-gallery">
              <div
                v-for="(wallpaper, index) in staticWallpapers"
                :key="index"
                class="wallpaper-item"
                :class="{ active: isActive(wallpaper) }"
                @click="$emit('change', wallpaper)"
              >
                <img :src="wallpaper.thumb" :alt="wallpaper.name" />
                <div class="wallpaper-name">{{ wallpaper.name }}</div>
              </div>
            </div>
          </div>
        </el-tab-pane>

        <!-- 动态壁纸 -->
        <el-tab-pane label="动态壁纸" name="dynamic">
          <div class="tab-content-wrapper">
            <div class="wallpaper-gallery">
              <div
                v-for="(wallpaper, index) in dynamicWallpapers"
                :key="index"
                class="wallpaper-item dynamic"
                :class="{ active: isActive(wallpaper) }"
                @click="$emit('change', wallpaper)"
              >
                <div class="dynamic-preview" :data-type="wallpaper.id">
                  <!-- 粒子星空效果 -->
                  <div v-if="wallpaper.id === 'particle'" class="particle-preview">
                    <div v-for="i in 30" :key="i" class="particle" 
                      :style="getParticleStyle(i)"></div>
                  </div>
                  
                  <!-- 气泡效果 -->
                  <div v-else-if="wallpaper.id === 'bubble'" class="bubble-preview">
                    <div v-for="i in 15" :key="i" class="bubble" 
                      :style="getBubbleStyle(i)"></div>
                  </div>
                  
                  <!-- 波浪效果 -->
                  <div v-else-if="wallpaper.id === 'wave'" class="wave-preview">
                    <div class="wave wave1"></div>
                    <div class="wave wave2"></div>
                    <div class="wave wave3"></div>
                  </div>
                  
                  <!-- 矩阵雨效果 -->
                  <div v-else-if="wallpaper.id === 'matrix'" class="matrix-preview">
                    <div v-for="i in 20" :key="i" class="matrix-col" 
                      :style="getMatrixStyle(i)"></div>
                  </div>
                  
                  <!-- 几何图形效果 -->
                  <div v-else-if="wallpaper.id === 'geometry'" class="geometry-preview">
                    <div v-for="i in 12" :key="i" class="shape" 
                      :style="getGeometryStyle(i)"></div>
                  </div>
                  
                  <!-- 默认图标 -->
                  <div v-else class="default-preview" :style="{ background: wallpaper.preview }">
                    <el-icon :size="40" color="#fff">
                      <component :is="wallpaper.icon" />
                    </el-icon>
                  </div>
                </div>
                <div class="wallpaper-name">{{ wallpaper.name }}</div>
              </div>
            </div>
          </div>
        </el-tab-pane>
      </el-tabs>
    </div>
  </el-dialog>
</template>

<script setup>
import { computed } from 'vue'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  currentWallpaper: {
    type: Object,
    required: true
  },
  staticWallpapers: {
    type: Array,
    required: true
  },
  dynamicWallpapers: {
    type: Array,
    required: true
  }
})

const emit = defineEmits(['update:modelValue', 'change'])

const visible = computed({
  get: () => props.modelValue,
  set: (value) => emit('update:modelValue', value)
})

const activeTab = computed(() => {
  return props.currentWallpaper.type === 'image' ? 'static' : 'dynamic'
})

const isActive = (wallpaper) => {
  if (wallpaper.type === 'image') {
    return props.currentWallpaper.type === 'image' && props.currentWallpaper.url === wallpaper.url
  } else {
    return props.currentWallpaper.type === 'dynamic' && props.currentWallpaper.id === wallpaper.id
  }
}

// 生成粒子样式
const getParticleStyle = (index) => {
  const size = Math.random() * 3 + 1
  const left = Math.random() * 100
  const top = Math.random() * 100
  const delay = Math.random() * 3
  const duration = Math.random() * 3 + 2
  
  return {
    width: `${size}px`,
    height: `${size}px`,
    left: `${left}%`,
    top: `${top}%`,
    animationDelay: `${delay}s`,
    animationDuration: `${duration}s`
  }
}

// 生成气泡样式
const getBubbleStyle = (index) => {
  const size = Math.random() * 40 + 20
  const left = Math.random() * 100
  const delay = Math.random() * 5
  const duration = Math.random() * 5 + 5
  const colors = ['rgba(255, 182, 193, 0.6)', 'rgba(173, 216, 230, 0.6)', 
                  'rgba(255, 218, 185, 0.6)', 'rgba(221, 160, 221, 0.6)']
  
  return {
    width: `${size}px`,
    height: `${size}px`,
    left: `${left}%`,
    background: colors[index % colors.length],
    animationDelay: `${delay}s`,
    animationDuration: `${duration}s`
  }
}

// 生成矩阵雨样式
const getMatrixStyle = (index) => {
  const left = (index * 5) % 100
  const delay = Math.random() * 2
  const duration = Math.random() * 2 + 1
  
  return {
    left: `${left}%`,
    animationDelay: `${delay}s`,
    animationDuration: `${duration}s`
  }
}

// 生成几何图形样式
const getGeometryStyle = (index) => {
  const size = Math.random() * 40 + 30
  const left = Math.random() * 100
  const top = Math.random() * 100
  const delay = Math.random() * 3
  const duration = Math.random() * 4 + 3
  const rotation = Math.random() * 360
  
  return {
    width: `${size}px`,
    height: `${size}px`,
    left: `${left}%`,
    top: `${top}%`,
    animationDelay: `${delay}s`,
    animationDuration: `${duration}s`,
    transform: `rotate(${rotation}deg)`
  }
}
</script>

<style scoped>
.wallpaper-dialog-content {
  height: 65vh;
  min-height: 500px;
  max-height: 700px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.fixed-tabs {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
}

/* Tab 内容包裹器 */
.tab-content-wrapper {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  padding: 12px;
}

.wallpaper-gallery {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  padding: 0 8px 0 0;
  overflow-y: auto;
  overflow-x: hidden;
  flex: 1;
  align-content: flex-start;
  min-height: 0;
}

.wallpaper-item {
  cursor: pointer;
  border-radius: 8px;
  overflow: hidden;
  transition: all 0.3s;
  border: 3px solid transparent;
  display: flex;
  flex-direction: column;
  flex: 1 1 200px;
  min-width: 200px;
  max-width: 250px;
}

.wallpaper-item:hover {
  transform: scale(1.05);
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}

.wallpaper-item.active {
  border-color: #0078d4;
  box-shadow: 0 8px 24px rgba(0, 120, 212, 0.3);
}

.wallpaper-item img {
  width: 100%;
  height: 120px;
  object-fit: cover;
  display: block;
  flex-shrink: 0;
}

.wallpaper-name {
  padding: 8px;
  text-align: center;
  font-size: 13px;
  color: #606266;
  background: #f5f5f5;
  flex-shrink: 0;
}

/* 动态壁纸预览 */
.dynamic-preview {
  width: 100%;
  height: 120px;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  overflow: hidden;
  flex-shrink: 0;
}

/* 粒子星空效果 */
.particle-preview {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  position: relative;
  overflow: hidden;
}

.particle {
  position: absolute;
  background: white;
  border-radius: 50%;
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.8);
  animation: twinkle linear infinite;
}

@keyframes twinkle {
  0%, 100% { opacity: 0; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.2); }
}

/* 气泡效果 */
.bubble-preview {
  width: 100%;
  height: 100%;
  background: linear-gradient(180deg, #e0f7fa 0%, #b2ebf2 50%, #80deea 100%);
  position: relative;
  overflow: hidden;
}

.bubble {
  position: absolute;
  bottom: -60px;
  border-radius: 50%;
  animation: float linear infinite;
}

@keyframes float {
  0% {
    bottom: -60px;
    opacity: 0;
  }
  10% {
    opacity: 0.8;
  }
  90% {
    opacity: 0.8;
  }
  100% {
    bottom: 120%;
    opacity: 0;
  }
}

/* 波浪效果 */
.wave-preview {
  width: 100%;
  height: 100%;
  background: linear-gradient(180deg, #4facfe 0%, #00f2fe 100%);
  position: relative;
  overflow: hidden;
}

.wave {
  position: absolute;
  bottom: 0;
  left: 0;
  width: 200%;
  height: 100%;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 1200 120'%3E%3Cpath d='M321.39,56.44c58-10.79,114.16-30.13,172-41.86,82.39-16.72,168.19-17.73,250.45-.39C823.78,31,906.67,72,985.66,92.83c70.05,18.48,146.53,26.09,214.34,3V0H0V27.35A600.21,600.21,0,0,0,321.39,56.44Z' fill='rgba(255,255,255,0.1)'/%3E%3C/svg%3E");
  background-size: 600px 120px;
  animation: wave 8s linear infinite;
}

.wave2 {
  animation-duration: 10s;
  opacity: 0.5;
  bottom: 10px;
}

.wave3 {
  animation-duration: 12s;
  opacity: 0.3;
  bottom: 20px;
}

@keyframes wave {
  0% { transform: translateX(0); }
  100% { transform: translateX(-50%); }
}

/* 矩阵雨效果 */
.matrix-preview {
  width: 100%;
  height: 100%;
  background: #000;
  position: relative;
  overflow: hidden;
}

.matrix-col {
  position: absolute;
  top: -100%;
  width: 2px;
  height: 100%;
  background: linear-gradient(transparent, #0f0);
  animation: matrix-fall linear infinite;
}

@keyframes matrix-fall {
  0% {
    top: -100%;
    opacity: 1;
  }
  100% {
    top: 100%;
    opacity: 0;
  }
}

/* 几何图形效果 */
.geometry-preview {
  width: 100%;
  height: 100%;
  background: linear-gradient(135deg, #f093fb 0%, #f5576c 100%);
  position: relative;
  overflow: hidden;
}

.shape {
  position: absolute;
  border: 2px solid rgba(255, 255, 255, 0.3);
  animation: rotate-shape linear infinite;
}

.shape:nth-child(odd) {
  border-radius: 0;
}

.shape:nth-child(even) {
  border-radius: 50%;
}

@keyframes rotate-shape {
  0% { transform: rotate(0deg); opacity: 0.3; }
  50% { opacity: 0.8; }
  100% { transform: rotate(360deg); opacity: 0.3; }
}

/* 默认预览样式 */
.default-preview {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

:deep(.el-dialog) {
  border-radius: 12px;
  overflow: hidden;
}

:deep(.el-dialog__body) {
  padding: 10px 20px 20px;
  overflow: hidden;
  height: 100%;
}

:deep(.el-tabs__header) {
  margin-bottom: 0;
  flex-shrink: 0;
}

:deep(.el-tabs__nav-wrap) {
  padding: 0 10px;
}

:deep(.el-tabs__content) {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

:deep(.el-tab-pane) {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

/* 美化滚动条 */
.wallpaper-gallery::-webkit-scrollbar {
  width: 8px;
}

.wallpaper-gallery::-webkit-scrollbar-track {
  background: #f1f1f1;
  border-radius: 4px;
}

.wallpaper-gallery::-webkit-scrollbar-thumb {
  background: #c1c1c1;
  border-radius: 4px;
}

.wallpaper-gallery::-webkit-scrollbar-thumb:hover {
  background: #a8a8a8;
}
</style>

